package com.yswy.common.utils;


import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @ Description   :  对象集合操作类
 */
@Component
public class EntityCollectionUtil {
    /**
     * @Description  ：返回所有实体对象集合
     * @exception    :
     */
    public List<Integer> getIdsByList(List<?> list, String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        if(!list.isEmpty()){
            Object ot = null;
            try {
                ot = Class.forName(className).newInstance();
            } catch (InstantiationException e) {
                throw new InstantiationException(e.getMessage());
            } catch (IllegalAccessException e) {
                throw new IllegalAccessException(e.getMessage());
            } catch (ClassNotFoundException e) {
                throw new ClassNotFoundException(e.getMessage());
            }
            Class c = ot.getClass();
            Field[] fields = c.getDeclaredFields();//取得所有类成员变量

            //取消每个属性的安全检查
            for(Field f:fields) {
                f.setAccessible(true);
            }

            List<Integer> ids = new ArrayList<>();

            for(int j = 0; j < list.size(); j++){
                for (int i = 0; i < fields.length; i++){
                    try {
                        if(fields[i].getName().equals("id")){
                            ids.add((Integer) fields[i].get(list.get(j)));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return ids;
        }
        return null;
    }


    public String[] getArrByList(List<?> list, String className,String fieldName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        if(list.isEmpty()){
            throw new IllegalArgumentException("传入对象集合为空");
        }
        String[] arr = new String[list.size()];
        Object ot = null;
        try {
            ot = Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            throw new InstantiationException(e.getMessage());
        } catch (IllegalAccessException e) {
            throw new IllegalAccessException(e.getMessage());
        } catch (ClassNotFoundException e) {
            throw new ClassNotFoundException(e.getMessage());
        }
        Class c = ot.getClass();
        Field[] fields = c.getDeclaredFields();//取得所有类成员变量

        //取消每个属性的安全检查
        for(Field f:fields) {
            f.setAccessible(true);
        }

        for(int j = 0; j < list.size(); j++){
            for (int i = 0; i < fields.length; i++){
                try {
                    if(fields[i].getName().equals(fieldName)){
                        arr[j] = (String) fields[i].get(list.get(j));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return arr;
    }
}
